home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / unix / next / nsort.c < prev    next >
C/C++ Source or Header  |  1996-06-25  |  1KB  |  82 lines

  1. #ifdef NeXT
  2. #include <sys/types.h>
  3. #include <sys/mman.h>
  4. #include <mach/mach.h>
  5. #include <fcntl.h>
  6. #include <sys/file.h>
  7. #include <sys/vnode.h>
  8. #endif
  9.  
  10. #include "cb.h"
  11.  
  12. main(argc,argv)
  13. int argc;
  14. char *argv[];
  15. {
  16.     int cmp_node();
  17.     caddr_t mc;
  18.     caddr_t mi;
  19.     caddr_t end;
  20.     struct stat st;
  21.     struct nind *idx;
  22.     struct nind key;
  23.     struct nind *found;
  24.     int    dir;
  25.     long    p;
  26.     long    idsize;
  27.     int    nel;
  28.     FILE *fp;
  29.  
  30.     int IS = sizeof(struct nind);
  31.  
  32.     if ((fp=fopen("nlist.ndx","r+")) == NULL)
  33.     {
  34.         printf("Error opening nlist.ndx\n");
  35.         exit(1);
  36.     }
  37.     fstat(fileno(fp),&st);
  38.     if ((mi = (caddr_t)mmap(NULL,st.st_size,PROT_READ|PROT_WRITE,MAP_SHARED,
  39.         fileno(fp),NULL)) == (caddr_t)NULL)
  40.     {
  41.         fprintf(stderr,"Error - mmap index file\n");
  42.         exit(1);
  43.     }
  44.     fclose(fp);
  45.  
  46.     idsize = st.st_size/IS;
  47.     idx = (struct nind *)mi;
  48.  
  49.     qsort((char *)idx, idsize, sizeof(struct nind), cmp_node);
  50.  
  51. #ifdef NeXT
  52.  
  53. /*    mmap() under NeXTStep is not documented and it doesn't appear to
  54.     write the changed pages back to the file properly.  So, I kludged
  55.     up a simple write.                        */
  56.  
  57.     printf("Writing sorted index file.\n");
  58.     {
  59.     int i;
  60.     if((i=open("nlist.ndx",O_CREAT|O_WRONLY,VREAD|VWRITE))<0) {
  61.        fprintf(stderr,"OPEN ERROR\n");
  62.        perror("WHY:");
  63.        exit(1);
  64.         }
  65.     write(i,mi,st.st_size);
  66.     close(i);
  67.     }
  68. #endif
  69.  
  70.     munmap(mi,st.st_size);
  71.     
  72.     exit(0);
  73.  
  74. }
  75.  
  76. cmp_node(i1,i2)
  77. struct nind *i1, *i2;
  78. {
  79.     return(strcmp(i1->name,i2->name));
  80. }
  81.  
  82.